//
//  SplicingTemplateCollectionViewCell.m
//  QHCollectionDemo
//
//  Created by MOiMac on 2021/3/8.
//

#import "SplicingTemplateCollectionViewCell.h"
#import <Masonry/Masonry.h>

@implementation SplicingTemplateCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    UIColor *randomColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];   // 随机颜色
    self.backgroundColor = randomColor;
//    self.backgroundColor = [UIColor clearColor];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:24];
    titleLabel.textColor = [UIColor whiteColor];
    [self.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).mas_offset(20);
        make.centerX.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).mas_offset(130-88);
        make.height.mas_equalTo(42);
    }];
    self.titleLabel = titleLabel;
    
    CQTemplateContainerView *templateContentView = [[CQTemplateContainerView alloc] initWithFrame:CGRectZero initAnimate:CQTemplateInitAnimateNone];
    templateContentView.layer.cornerRadius = 20;
    templateContentView.layer.masksToBounds = YES;
    templateContentView.userInteractionEnabled = NO;    // 这里添加此行，只会防止其在collectionView中点击无效
    [self.contentView addSubview:templateContentView];
    [templateContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView).mas_offset(0);
        make.top.mas_equalTo(titleLabel.mas_bottom).mas_equalTo(214-172);
        make.bottom.mas_equalTo(self.contentView).mas_offset(-10);
    }];
    self.templateContentView = templateContentView;
}


#pragma mark - Setter
- (void)setImageWidthHeightRatio:(CGFloat)imageWidthHeightRatio {
    _imageWidthHeightRatio = imageWidthHeightRatio;
    
    [self.templateContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView).mas_offset(0);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_equalTo(214-172);
        make.bottom.mas_equalTo(self.contentView).mas_offset(-10);
    }];
}


- (void)updateImages:(NSArray<UIImage *> *)images styleIndex:(NSInteger)styleIndex {
    [self.templateContentView updateImagesByDataModels:images styleIndex:styleIndex];
    
    NSString *templateTitle = self.templateContentView.realTemplateModel.title;
    self.titleLabel.text = templateTitle;
}


- (void)updateStyleIndex:(NSInteger)styleIndex {
    [self.templateContentView updateStyleIndex:styleIndex];
}


@end
